package com.guxingke.typecho4j.widget.handle.meta;

import com.guxingke.typecho4j.Typecho4jException;
import com.guxingke.typecho4j.route.DispatcherStrategy;
import com.guxingke.typecho4j.route.Route;
import com.guxingke.typecho4j.route.Routes;
import com.guxingke.typecho4j.widget.WidgetHandle;
import com.guxingke.typecho4j.widget.provider.MetaBaseWidget;
import com.guxingke.typecho4j.widget.provider.ProviderWidget;
import com.guxingke.typecho4j.widget.provider.meta.CategoryWidget;
import com.jfinal.core.Controller;

public class CategoryDeleteHandleWidget extends MetaBaseWidget implements
		WidgetHandle {

	@Override
	public void configRoute(Routes routes) {
		routes.addRoute(new Route("deleteCategory", this.getClass(),
				"/admin/manage-categories",
				DispatcherStrategy.REDIRECT));
	}

	@Override
	public void handle(Controller controller) throws Typecho4jException {
		// merge=1 do=deleteCategory mid[][]={1,4}
		Long merge = controller.getParaToLong("merge");
		String[] mids = controller.getParaValues("mid[]");
		Long mid = null;
		for (String string : mids) {
			mid = Long.parseLong(string);
			if (merge.equals(mid)) {
				continue;
			} else {
				mergeCategory(mid, merge);
				deleteCategory(mid);
			}
		}

		ProviderWidget.refreshProviderWidget(CategoryWidget.class);
	}

}
